import * as PIXI from 'pixi.js';
/**
 * 根据时间的补间动画
 * @param ticker 动画触发
 * @param from 从
 * @param to 至
 * @param time 时间
 * @param update 修改
 */
export function timeTween(
  ticker: PIXI.Ticker,
  from: number[],
  to: number[],
  time: number,
  update: (data: number[]) => void,
) {
  const start = performance.now();
  const space: number[] = [];
  from.forEach((v, i) => space.push(to[i] - v));
  // 动态修改
  const t = () => {
    const b = (performance.now() - start) / time;
    const data = Object.assign([], space);
    data.forEach((v, i) => {
      if (v !== 0) {
        data[i] = v * b + from[i];
      }
    })
    update(data);
  }
  ticker.add(t);
  return new Promise<void>((resolve) => {
    setTimeout(() => {
      ticker.remove(t);
      update(to);
      resolve();
    }, time);
  })
}
